package com.cloud.springcloudhysyrix.hebingqingqiu;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

/**
 *
 * 注解方式请求合并
 *
 * weiximei on 2018/6/2
 */
@Service
public class WxmService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * batchMethod 表示后发起的请求
     * collapserProperties 配置一些默认的参数。
     *          timerDelayInMilliseconds 表示多少毫秒内的请求的进行合并
     * @param id
     * @return
     */
    @HystrixCollapser(batchMethod = "getWxm"
            ,collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds",value = "200")})
    public Future<String> batchGetWxm(long id){
        return null;
    }

    /**
     * 合并后发起请求
     * @param ids
     * @return
     */
    @HystrixCommand
    public List<String> getWxm(List<Long> ids){

        System.out.println("发起请求。。参数为："+ids.toString());
        String [] result = restTemplate.getForEntity("http://HELLO-SERVER/weiximei2?ids={1}",String[].class,StringUtils.join(ids,",")).getBody();


        return Arrays.asList(result);
    }

}
